package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.thirdpost.mapper.BusProcureApplyJudgeMapper;
import com.jdjw.modules.thirdpost.param.BusProcureApplyJudgeParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.modules.thirdpost.result.BusProcureApplyJudge;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * {"code":"200","msg":"入库数据量为0，请检查必传字段","data":null}
 * 业主评委信息
 */
@Slf4j
@Service
public class BusProcureApplyJudgeServiceImpl extends ServiceImpl<BusProcureApplyJudgeMapper, BusProcureApplyJudge>
        implements IService<BusProcureApplyJudge> {

    @Autowired
    static BusProcureApplyJudgeMapper busProcureApplyJudgeMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        BusProcureApplyJudgeServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setBusProcureApplyJudgeMapper(BusProcureApplyJudgeMapper busProcureApplyJudgeMapper) {
        BusProcureApplyJudgeServiceImpl.busProcureApplyJudgeMapper = busProcureApplyJudgeMapper;
    }


    public static void doPostBusProcureZishiqi(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 17;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 17;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 17;
        }
        try {
            //查询
            List<BusProcureApplyJudge> list = busProcureApplyJudgeMapper.selectListCha();
            if (list == null || list.size() < 1) {
                log.info("业主评委信息数据库查询数据为0条");
                return;
            }
            if (list.size() > 3000) {
                Integer count = list.size() / 3000 + 1;
                Collection<Collection<BusProcureApplyJudge>> fgList = CommonUtil.subCollection(list, count);
                for (Collection<BusProcureApplyJudge> obj : fgList) {
                    sendData((List<BusProcureApplyJudge>) obj, url);
                }
            } else {
                sendData(list, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void sendData(List<BusProcureApplyJudge> obj, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (BusProcureApplyJudge a : obj) {
            BusProcureApplyJudgeParam param = new BusProcureApplyJudgeParam();
            param.setBiz_id(a.getApplyId());
            param.setParent_id(a.getApplyId());
            param.setCwh(a.getCwh());
            param.setCwhxzqh(CommonUtil.addZeroForNum(a.getCwhxzqh(), 16));
            param.setYzpwmc(a.getName());
            param.setYzpwzjhm(a.getIdCard());
            param.setDel_flag(0);

            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("业主评委信息数据推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostBusProcureZishiqi", "业主评委信息数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("业主评委信息推送失败", result);
            }
        } else {
            log.info("业主评委信息推送失败", "第三方返回空");
        }
    }
}
